from lxml import etree
doc='''
<div>
    <ul class='ul items'>
         <li class="item-0"><a href="link1.html">first item</a></li>
         <li class="item-1"><a href="link2.html"><span style="font-size:20px;">second item</span></a></li>
         <li class="item-inactive"><a href="link3.html">third item</a></li>
         <li class="item-1"><a href="link4.html">fourth item</a></li>
         <li class="item-0"><a href="link5.html">fifth item</a> # 注意，此处缺少一个 </li> 闭合标签
     </ul>
</div>
'''
html=etree.HTML(doc)
# 定位元素
print(etree.tostring(html.xpath("//li[@class='item-inactive']")[0]))
print(html.xpath("//li[@class='item-inactive']")[0].text) #因为第三个li下面没有直接text，None
print(html.xpath("//li[@class='item-inactive']/a")[0].text)
print(html.xpath("//li[@class='item-inactive']/a/text()"))
print(html.xpath("//li[@class='item-inactive']/.."))
print(html.xpath("//li[@class='item-inactive']/../li[@class='item-0']"))
# contions
print(html.xpath("//*[contains(@class,'item')]"))
# starts-with
print(html.xpath("//*[starts-with(@class,'ul')]"))

#最后一个li被限定了
print(html.xpath("//li[last()]/a/text()"))
#会得到所有的`<a>`元素的内容，因为每个<a>标签都是各自父元素的最后一个元素。
#本来每个li就只有一个<a>子元素，所以都是最后一个
print(html.xpath("//li/a[last()]/text()"))
print(html.xpath("//li/a[contains(text(),'third')]"))
print(html.xpath("//li[position()=4]/a/text()"))
print(html.xpath("//ul/li[@class='item-inactive']/node()"))
print(html.xpath("//ul/node()"))


# 获取内容
print(html.xpath("//a/text()"))
print(html.xpath("//a")[0].text)
print(html.xpath("//ul")[0].text)
print(len(html.xpath("//ul")[0].text))
print(html.xpath("//ul/text()"))
# 获取属性
print(html.xpath("//a/@href"))
print(html.xpath("//li/@class"))
# 获取标签名
print(html.xpath("//*[@class='ul items']")[0].tag)
